<?

/*	MediaNavigation OBJECT
*	Methods:
*	output
*
*	Constructor:
*	
*
*					*/


class MediaNavigation extends MOD {

	function __construct( $vars ) {
		$this->_addvars( $vars );
		$this->_ref();
	}
	
	function output() {
		
		if( $this->action )
			$result = $this->{ "filter_" . $this->action }();

			
		return $this->parse( $result ) . $this->render_js();
		
	}
	
	function ajax() {
	
		if( $this->action )
			$result = $this->{ "ajax_" . $this->action }();
			
		return $result;
	
	}
	
	
	/////////////////////////////////////
	/////// MODULE "WINDOW" CALLS (html output)
	
	private function filter_viewmaster() {
		global $DB;

		
		$this->nameid = $DB->get_key( $this->vars->type, "ID=". $DB->smartq( $_GET['id'], "int" ), "nameid" );
		
		if( $query = $DB->reference( $this->vars->type, "parentid=". $DB->smartq( $_GET['id'], "int" ), $this->vars->objtype ) )
			foreach ( $query as $node ) $set[]["id"] = $node;
			
		
		$this->js = "
			target = $$( '.MediaNavigation' ).pop();

			target.insert(
				mod = new MediaNavigation( ". json_encode( $this->vars ) .", ". json_encode( $set ) ." )
			);
			
			Event.observe( window, 'load', mod.navigation_viewmaster.bind(mod) );
			
		";
		
		/*$nodes = $DB->filter_key(
			$this->vars->parenttype,
			"active order by rank desc, flog desc",
			array( "ID", "<%lang:nameid%> as name", "(select count(*) from <%table:glue%> as glue where glue.parentid=mod_object.ID && glue.parenttype=<%type:{$this->vars->parenttype}%>) as count" ) );
		
		if( $nodes ) 
			foreach($nodes as $nkey => $node ) {
				$nodes[ $nkey ] = $this->render_album( $node );
				if( $thumbs = $DB->reference( $this->vars->parenttype, "parentid=". $node['ID'] ." order by rand() limit 3", $this->vars->objtype ))
					$js_target[] = array( "id"=> $node['ID'], "children"=> implode(",", $thumbs )); 
			}


		$result = ( $nodes ) ?
			implode("\n", $nodes ) . "<div class='stable'></div>
			<script type='text/javascript'>
				overview = new MediaAlbum_overview( ". json_encode( $js_target ) .", ". json_encode( $this->vars ) ." );
			</script>"  : "empty.";
		*/

		return $result;
	}
	
	private function render_album( $node ) {

		return "<span class='album'><div id='album." . $node['ID'] . "'></div><span class='name'>". $node["name"] . "</span><span class='descr'>(". $node['count'] ." image" .(((int) $node['count'] > 1)? "s":""). ")</span></span>";
	}
	

	/////////////////////////////////////
	/////// MODULE AJAX CALLS (js output)
	
	function ajax_pull() {
		// THIS ONE GETS PARSED, SEND JSON STRINGS ONLY. 
		global $DB;
		
		if( $group = $DB->reference( $this->vars->parenttype, "parentid = ".$DB->smartq( $this->vars->id, "int" ), $this->vars->objtype ))
			$images = $DB->filter_key( $this->vars->objtype, "ID in(". implode( ",", $group ) .")", "ID as id" );
			
		
		return json_encode( $images );
	}
}

?>